package com.example.keli.ui.list;

import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseFragment;
import com.example.keli.base.utils.DensityUtil;
import com.kennyc.view.MultiStateView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

/**
 * description:
 * author: Allen
 * date: 2019/3/21 18:34
 */
public class RankFragment extends BaseFragment {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout mRefreshlayout;
    int page = 1;
    RankListAdapter adapter;
    List<RankData.DataBean> mListData = new ArrayList<>();
    @BindView(R.id.multiStateView)
    MultiStateView multiStateView;

    @Override
    protected int layoutId() {
        return R.layout.fragment_rank;
    }

    @Override
    protected void bindData() {
        adapter = new RankListAdapter(mListData, getActivity());
        mRecyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerview.setAdapter(adapter);
        mRecyclerview.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(getActivity())
                        .color(ContextCompat.getColor(getActivity(), R.color.divider_line))
                        .size(DensityUtil.dip2px(getActivity(), 10))
                        .build());
        multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        getAnnouncement();
    }

    @Override
    protected void bindListener() {
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getAnnouncement();
            }
        }, mRecyclerview);
        mRefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mListData.clear();
                getAnnouncement();
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
    }

    private void getAnnouncement() {
        Map<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        HttpUtils.ApiFunc(RetrofitManager.mApiFunc.getAnnouncement(map), new SubscriberListener<BaseResponse<RankData>>() {
            @Override
            public void onSuccess(BaseResponse<RankData> response) {
                if (page == 1) {
                    mRefreshlayout.setRefreshing(false);
                    if (response.getData().getData().size() == 0) {
                        multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
                        return;
                    }
                }
                multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                if (response.getData().getData().size() == 0) {
                    adapter.loadMoreEnd();
                    return;
                }
                mListData.addAll(response.getData().getData());
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }

}
